/**
 * @file page_display.cpp
 * @author Forairaaaaa
 * @brief
 * @version 0.1
 * @date 2023-11-18
 *
 * @copyright Copyright (c) 2023
 *
 */
#include "../app_settings.h"
#include "../../utils/system/ui/ui.h"
#include "../../utils//system/inputs/inputs.h"
#include "../../../hal/hal.h"
#include "spdlog/spdlog.h"
#include <cstdint>
#include <string>

using namespace MOONCAKE::APPS;
using namespace SYSTEM::UI;
using namespace SYSTEM::INPUTS;

void AppSettings::_page_logconfig()
{
    while (1)
    {
        std::vector<std::string> items = {"[LOG CONFIG]"};

        items.push_back("UART Baud: " + std::to_string(HAL::GetSystemConfig().baud_uart));
        items.push_back("CAN  Baud: " + std::to_string(HAL::GetSystemConfig().baud_can) + "K");
        items.push_back("MAX FSize: " + std::to_string(HAL::GetSystemConfig().file_max_size) + "MB");

        items.push_back(HAL::GetSystemConfig().add_time_to_log_uart == 0 ? "UART TimeStamp: OFF" : "UART TimeStamp: ON");
        items.push_back(HAL::GetSystemConfig().add_time_to_log_can == 0 ?  "CAN  TimeStamp: OFF" : "CAN  TimeStamp: ON");
        items.push_back(HAL::GetSystemConfig().rec_uart_flag == 0 ?        "UART Record On: OFF" : "UART Record On: ON");
        items.push_back(HAL::GetSystemConfig().rec_can_flag == 0 ?         "CAN  Record On: OFF" : "CAN  Record On: ON");

        items.push_back("Back");

        auto selected = _data.select_menu->waitResult(items);

        // Button sound
        if (selected == 1)
        {
            std::vector<std::string> items = {"[UART BAUD CONFIG]"};
            items.push_back("9600");
            items.push_back("115200");
            items.push_back("460800");
            items.push_back("921600");
            items.push_back("Back");
            auto selected = _data.select_menu->waitResult(items);
            uint32_t baud = 115200;
            if (selected == 1)
                baud = 9600;
            else if (selected == 2)
                baud = 115200;
            else if (selected == 3)
                baud = 460800;
            else if (selected == 4)
                baud = 921600;
            HAL::GetSystemConfig().baud_uart = baud;
            _data.is_config_changed = true;
        }
        else if (selected == 2)
        {
            std::vector<std::string> items = {"[CAN BAUD CONFIG]"};
            items.push_back("125 Kbps");
            items.push_back("500 Kbps");
            items.push_back("1   Mbps");
            items.push_back("Back");
            auto selected = _data.select_menu->waitResult(items);
            uint32_t baud = 1000;
            if (selected == 1)
                baud = 125;
            else if (selected == 2)
                baud = 500;
            else if (selected == 3)
                baud = 1000;
            HAL::GetSystemConfig().baud_can = baud;
            _data.is_config_changed = true;
        }
        else if (selected == 3)
        {
            std::vector<std::string> items = {"[FILE MAX SIZE CONFIG]"};
            items.push_back("10  MB");
            items.push_back("50  MB");
            items.push_back("100 MB");
            items.push_back("200 MB");
            items.push_back("500 MB");
            items.push_back("1   GB");
            items.push_back("Back");
            auto selected = _data.select_menu->waitResult(items);
            uint16_t file_max_size = 10; // mb
            if (selected == 1)
                file_max_size = 10;
            else if (selected == 2)
                file_max_size = 50;
            else if (selected == 3)
                file_max_size = 100;
            else if (selected == 4)
                file_max_size = 200;
            else if (selected == 5)
                file_max_size = 500;
            else if (selected == 6)
                file_max_size = 1000;
            HAL::GetSystemConfig().file_max_size = file_max_size;
            _data.is_config_changed = true;
        }

        //
        else if (selected == 4)
        {
            HAL::GetSystemConfig().add_time_to_log_uart = (HAL::GetSystemConfig().add_time_to_log_uart == 0 ? 1 : 0);
            _data.is_config_changed = true;
        }
        else if (selected == 5)
        {
            HAL::GetSystemConfig().add_time_to_log_can = (HAL::GetSystemConfig().add_time_to_log_can == 0 ? 1 : 0);
            _data.is_config_changed = true;
        }
        else if (selected == 6)
        {
            HAL::GetSystemConfig().rec_uart_flag = (HAL::GetSystemConfig().rec_uart_flag == 0 ? 1 : 0);
            _data.is_config_changed = true;
        }
        else if (selected == 7)
        {
            HAL::GetSystemConfig().rec_can_flag = (HAL::GetSystemConfig().rec_can_flag == 0 ? 1 : 0);
            _data.is_config_changed = true;
        }
        else
            break;
    }
}
